import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

void main() throws SQLException, ClassNotFoundException {
    Scanner sc = new Scanner(System.in);

    OUTER:
    while (true) {
        System.out.println("-----欢迎光临图书馆！-----");
        System.out.println("请选择需要执行操作的序号：");
        System.out.println("1.单本书籍信息的查询");
        System.out.println("2.展示所有书籍");
        System.out.println("3.上架书籍");
        System.out.println("4.下架书籍");
        System.out.println("5.退出应用");
        int choice = sc.nextInt();

        switch (choice) {
            case 1 -> {
                System.out.println("单本书籍信息的查询>>>>>");
                ArrayList<Book> bookList = MysqlBook.findBookList();

                if (bookList.isEmpty()) {
                    System.out.println("图书馆暂无书籍，请先上架书籍！");
                    continue;
                }

                Book book = MysqlBook.findBookById();
                if (book == null) {
                    continue;
                }
                System.out.printf("编号(%d)的书籍是：《%s》%n", book.getId(), book.getTitle());
            }

            case 2 -> {
                System.out.println("展示所有书籍>>>>>");

                ArrayList<Book> bookList = MysqlBook.findBookList();

                if (!bookList.isEmpty()) {
                    bookList.sort(Comparator.comparing(Book::getId));
                    for (Book book : bookList) {
                        System.out.printf("书籍编号：%d，书籍名称：《%s》，书籍作者：%s，书籍价格：%.2f%n", book.getId(), book.getTitle(), book.getAuthor(), book.getPrice());
                    }
                } else {
                    System.out.println("图书馆暂无书籍，请先上架书籍！");
                }
            }

            case 3 -> {
                System.out.println("上架书籍>>>>>");

                int add = MysqlBook.addBook();

                if (add == -1) {
                    continue;
                }
                if (add > 0) {
                    System.out.println("书籍上架成功！");
                } else {
                    System.out.println("书籍上架失败！");
                }
            }

            case 4 -> {
                System.out.println("下架书籍>>>>>");
                ArrayList<Book> bookList = MysqlBook.findBookList();

                if (bookList.isEmpty()) {
                    System.out.println("图书馆暂无书籍，请先上架书籍！");
                    continue;
                }

                boolean isDelete = MysqlBook.deleteBook();
                if (isDelete) {
                    System.out.println("书籍下架成功！");
                }
            }

            case 5 -> {
                System.out.println("退出应用>>>>>");
                break OUTER;
            }

            default -> System.out.println("输入的序号错误，请输入(1-4)之内的数字序号！");
        }
    }
}